package com.allwees.bs.c.module.user.repository;

import com.allwees.bs.c.module.user.entity.ProductCollectionEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;

public interface ProductCollectionRepository extends JpaRepository<ProductCollectionEntity, String> {
    @Query(value = "SELECT e FROM ProductCollectionEntity e WHERE e.userUuid = :userUuid order by e.id desc",
            countQuery = "SELECT COUNT(1) FROM ProductCollectionEntity  WHERE userUuid = :userUuid")
    Page<ProductCollectionEntity> findCollProduct(@Param("userUuid") String userUuid, Pageable pageable);

    @Modifying
    @Query(value = "delete from ProductCollectionEntity where userUuid=:userUuid and productUuid in :productUuids")
    void cancelForUser(@Param("userUuid") String userUuid, @Param("productUuids") List<String> productUuids);

    @Query(value = "select productUuid from ProductCollectionEntity where userUuid=:userUuid and productUuid in :productUuids")
    Set<String> findCollProductUuids(@Param("userUuid") String userUuid, @Param("productUuids") Set<String> productUuids);

    List<ProductCollectionEntity> findAllByUserUuid(String userUuid);
}
